<?php
/**
 * 确认计划
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Setting");
apf_require_class("Bll_Jx_Plan");
apf_require_class("Bll_Jx_Relation");
class Team_QueryPlanInfoController extends AbstractController {

    public function handle_request() {
        $this->team_bll = new Bll_Jx_Relation();

        //这次考评的最后评分日
        $this->setting_bll = new Bll_Jx_Setting();
        $this->tbll = new Bll_Jx_Plan();
        $is_last_date = $this->setting_bll->get_current_check();

        $last_month = $this->setting_bll->format_performance();
        //下属列表
        $sub_list = $this->get_lower_list();
        //271分布,这里不算免考和新入职的
        $rel_bll = new Bll_Jx_Relation();
        $no_kp = $rel_bll->get_kp_user(1);
        $no_kp[4183]=1;

        if($this->request->is_post_method()){//提交下属全部绩效
            if(!empty($sub_list)){
                $uids=array();
                foreach($sub_list as $val){
                    if(!empty($no_kp[$val['user_id']])){
                        continue;
                    }
                    $uids[] = $val['user_id'];
                }
                if(!empty($uids)){
                    $this->tbll->change_status($uids,$last_month[0],Const_JxStatus::OPT_ACTION_48);
                }
            }
        }
//tp($sub_list);
        //最这次和上次的考评

//print_r($last_month);
        $datalist = $this->build_lower_data($sub_list,$last_month);

        $status_list = Const_JxStatus::plan_status();
        //tp($no_kp);
        $score_stage = Const_JxStatus::score_stage();
        $max_users = 0;
        $abc_list = array();
        foreach($datalist as $uid=> $val){
            if(!empty($val[$last_month[0]])){//上次考核有计划的
                $max_users +=1;
                $score = empty($val['adjustScore']) ? $val['personScore'] : $val['adjustScore'];
                $str = Const_JxStatus::check_score_stage($score);
                $abc_list[$str] +=1;
            }
        }
        $this->request->set_attribute('no_kp',$no_kp);
        $this->request->set_attribute('max_users',$max_users);
        $this->request->set_attribute('abc_list',$abc_list);
        $this->request->set_attribute('datalist',$datalist);
        $this->request->set_attribute('is_last_date',$is_last_date);
        $this->request->set_attribute('last_month',$last_month);
        $this->request->set_attribute('sub_list',$sub_list);
        $this->request->set_attribute('status_list',$status_list);
        return 'Team_QueryPlanInfo';
    }
    private function build_lower_data($sub_list,$last_month){

        $uids = array();
        foreach ($sub_list as $value) {
            $uids[] = $value['user_id'];
        }
        $_where = array(
                "userId IN(".implode(',',$uids).") AND 1= ?"=>1
                );
        $m_list = array();
        foreach ($last_month as $val){
            if(!empty($val['m_list'])){
                $m_list[] = $val;
            }
        }
        if(empty($m_list)){
            return array();
        }
        $_where["serachMonth IN(".implode(',',$m_list).") AND 1= ?"]=1;

        //tp($_where);
        $data = $this->tbll->find_performance($_where);
        //tp($data);
        //按月归类
        $res = array();
        foreach($data as $val){
            $res[$val['userId']][$val['serachMonth']] = $val;
        }
        //tp($res);
        return $res;

    }
    /**
     * 过滤员工已离职的
     * @return Ambigous <multitype:, unknown>
     */
    private function get_lower_list(){
        $uid = $this->request->get_user_id();
        $sub_list = $this->team_bll->get_lower_user($uid);
        return $sub_list;
    }
}